import {Component, OnInit} from '@angular/core';
import {KeycloakService} from './shared/services/keycloak.service';
import {HttpClient} from '@angular/common/http';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.less'],
})
export class AppComponent implements OnInit {

  constructor(private http: HttpClient, private kc: KeycloakService) {

  }

  profile: User;
  private serviceUrl = '/testUnit/test';

  public message: string;
  public errorClass = '';

  private static handleResponse(res: Response, comp: AppComponent) {
    comp.errorClass = '';
    console.log('█ res ►►►', res);
    comp.message = 'Message: success';
  }

  private static handleServiceError(error: Response, comp: AppComponent) {
    comp.errorClass = 'error';
    if (error.status === 0) {
      comp.message = 'Request failed';
    } else {
      comp.message = error.status + ' ' + error.statusText;
    }
  }

  authenticated(): boolean {
    return KeycloakService.authenticated();
  }

  ngOnInit(): void {
    this.profile = KeycloakService.getUser();
  }

  login() {
    KeycloakService.login();
  }

  logout() {
    KeycloakService.logout();
  }

  account() {
    KeycloakService.account();
  }

  request() {
    this.http.get(this.serviceUrl)
      .subscribe((res: Response) => {
          console.log('█ res ►►►', res);
          AppComponent.handleResponse(res, this);
        },
        (error: Response) => {
          AppComponent.handleServiceError(error, this);
        });
  }

  skipToIde() {
    window.open('http://192.168.1.97:8033/ide');
  }
}
